package com.zklh.leetcode.other.problem19;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 坐看落花
 * @descrpiton 关于链表删除倒数第n个元素的实现
 * @date 2019/9/26
 */
public class Solution {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        List<ListNode> listNodes = new LinkedList<>();
        // 定义当前节点, 用于在遍历过程中, 指向遍历节点
        ListNode nowNode = head;
        while(nowNode != null){
            listNodes.add(nowNode);
            nowNode = nowNode.next;
        }
        int preIndex = listNodes.size() - n - 1;
        if(preIndex < 0){
            return head.next;
        }else{
            ListNode preNode = listNodes.get(preIndex);
            preNode.next = preNode.next.next;
            return head;
        }
    }


    /**
     * 节点
     */
    private class ListNode{
        int val;
        ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }


    public static void main(String[] args){
    }
}

